<?php
defined('PG_PAGE') or die();

class PGSendSMS 
{
	private $user_name	= 'onecard';
  private $password   = '123456';
  private $brandname  = 'ONECARD';
	private $url_send_sms	= 'http://pnt24h.com/ws/ws.php?wsdl';
	
	public function send($phone = '', $text = '', $orderId = 0, $order_type=0, $sms_id=0){
	  global $database;
    
    if (is_array($phone)){
      foreach ($phone as $sPhone) $this->send($sPhone, $text, $orderId, $order_type);
      return;
    }
    
		$phone = str_replace(array(' ','.',',','+'), '', $phone);
		$listValidPhone = array('09'=>'849','012'=>'8412','016'=>'8416','849'=>'849','0188'=>'84188','0199'=>'84199','8412'=>'8412','8416'=>'8416','84188'=>'84188','84199'=>'84199');
		$isNum  = false;
		$phoneNew = $phone;
		foreach($listValidPhone as $k=>$v){
			$num = strlen($k);
			$checkNum = substr($phone,0,$num);
			
			if($k == $checkNum){
				$isNum = true;
				$phoneNew = substr($phone,$num,strlen($phone));
				$phoneNew = $listValidPhone[$checkNum].$phoneNew;
				break;
			}
		}
		if(!$isNum){
		  $database->db_query("INSERT INTO sms_reports (sms_recipient, sms_content, sms_order_id, sms_sendtime, sms_status, sms_reponse) VALUES  ('%s', '%s', %d, %d, %d, '%s')", $phone, $text, $orderId, time(), 0, "invalid number");
			return('invalid number');
		}

		$text = self::strip_special_char(convertKhongdau($text));
		return $this->send_brandname($phoneNew, $text, $orderId, $order_type, $sms_id);
		
	}
	
	public function send_brandname($phone = '', $text = '', $orderId = 0, $order_type=0, $sms_id=0){
		global $database, $user;
    
    $text = str_replace("\r\n", "\n", $text);
    $text = str_replace("\r", "\n", $text);
    $text = str_replace("\n", ". ", $text);
		
    require_once('include/nusoap/nusoap.php');
		$client 	= new nusoap_client($this->url_send_sms, true);
		//$param = array('username'=>$this->user_name, 'password'=>$this->password, 'brandname'=>$this->isvt($phone)?'HONG PHUC':$this->brandname, 'listphone'=>$phone, 'content'=>$text);
    $param = array('username'=>$this->user_name, 'password'=>$this->password, 'brandname'=>$this->brandname, 'listphone'=>$phone, 'content'=>$text, 'typesms'=>1);

		$client->call('ws', $param);
		$return = $client->responseData;
    
    $p = xml_parser_create();
		xml_parse_into_struct($p, $return, $xml);
		$return = '';
		if($xml){
			$return = $xml[3]['value'];
		}
    
		if ($sms_id==0) $database->db_query("INSERT INTO sms_reports (sms_recipient, sms_content, sms_order_type, sms_order_id, sms_sendtime, sms_status, sms_response) VALUES  ('%s', '%s', %d, %d, %d, %d, '%s')", $phone, $text, $order_type, $orderId, time(), is_numeric($return)?0:1, $return);
      
    if (!is_numeric($return)){
      if ($sms_id>0) $database->db_query("UPDATE sms_reports SET sms_status=1, sms_response='%s', sms_sendtime=%d WHERE sms_id=%d", $return, time(), $sms_id);
      
      if ($order_type==0){
        $database->db_query("UPDATE orders SET order_sentsms=1 WHERE order_id=%d", $orderId);
        $database->db_query("UPDATE coupons SET coupon_sentsms=1 WHERE coupon_order_id=%d", $orderId);
      }elseif ($order_type==2 || $order_type==1){
        $database->db_query("UPDATE orders SET order_sentsms=1 WHERE order_id=%d", $orderId);
      }elseif ($order_type==3){
        $database->db_query("UPDATE user_orders SET uo_sentsms=1 WHERE uo_id=%d", $orderId);
      }
    }
		
		return !is_numeric($return);
	}
	
	private function isvt($phone = ''){
		$listPhone = array('097','098','016','8416','8497','8498');
		foreach($listPhone as $v){
			$num = strlen($v);
			$checkNum = substr($phone,0,$num);
			if($v == $checkNum){
				return true;
			}
		}
		return false;
	}
  
	private function ismb($phone = ''){
		$listPhone = array('090','093','0122','0126','0121','0128','0120','8490','8493','84122','84126','84121','84128','84120');
		foreach($listPhone as $v){
			$num = strlen($v);
			$checkNum = substr($phone,0,$num);
			if($v == $checkNum){
				return true;
			}
		}
		return false;
	}
	private function isvn($phone = ''){
		$listPhone = array('091','094','0123','0125','0127','8491','8494','84123','84125','84127');
		foreach($listPhone as $v){
			$num = strlen($v);
			$checkNum = substr($phone,0,$num);
			if($v == $checkNum){
				return true;
			}
		}
		return false;
	}
 	static function strip_special_char($t) {
		//$t = preg_replace("/[^a-zA-Z0-9\/\n\s\._,:;-]/", '', $t);
        return trim($t);
    }
}
?>